import {baseEffects, baseReducers, baseState} from "../../../../models/baseModel";
import Store from "../../../../utils/store";
import PageHelper from "utils/pageHelper";
import {changeLevel, getLevel} from "../../../../services/LogHttpService";
import Const from "../../../../common/Const";

export default {
  namespace: 'log',

  state: {
    ...baseState,
  },

  reducers: {
    ...baseReducers
  },

  effects: {

    ...baseEffects,

    * getPageInfo({payload}, {call, put}) {

      let packageNames = Store.getStore(Const.logSaveKey);
      // let packageNames = Store.getStore(Const.logSaveKey);
      console.log("logPackage", packageNames);

      if (!packageNames||packageNames == undefined) {
        let i = 0;
        packageNames = ['com.mlnx', 'org.shan', 'com.mlnx.bluwifi',
          'com.mlnx.data', 'com.mlnx.device', 'com.mlnx.device.dao'];
        Store.setStore(Const.logSaveKey, packageNames);
      }
      packageNames.sort((v1, v2) =>  v1.localeCompare(v2));

      const logPackages = [];
      for (let i = 0; i < packageNames.length; i++) {
        const packageName = packageNames[i];
        const apiPath = "/actuator/loggers/" + packageName;

        try {
          const resp = yield call(getLevel, apiPath);
          const {effectiveLevel, configuredLevel} = resp;
          logPackages.push({packageName, level: effectiveLevel})
        } catch (e) {
          console.error(e);
        }

      }

      yield put({
        type: 'list',
        payload: {
          data: PageHelper.wrapPage(logPackages)
        }
      });
    },

    // 保存和修改
    * save({payload}, {put, call}) {

      let packageNames = Store.getStore(Const.logSaveKey);
      if (!packageNames)
        packageNames = [];

      const {success, value} = payload;
      const {packageName, level} = value;

      let findFlag = false;
      for (let i = 0; i < packageNames.length; i++) {
        if (packageNames[i] == packageName) {
          findFlag = true;
          break;
        }
      }
      console.log(packageNames,packageName)
      if (!findFlag) {
        packageNames.push(packageName);
      }
      Store.setStore(Const.logSaveKey, packageNames);

      try {
        const apiPath = "/actuator/loggers/" + packageName;
        const resp = yield call(changeLevel, apiPath, {configuredLevel: level});
        success();
      } catch (e) {
        console.error(e);
      }

      yield put({
        type: 'getPageInfo',
        payload: {}
      });
    },


    // 删除 之后查询分页
    * delete({payload}, {call, put, select}) {

      let packageNames = Store.getStore(Const.logSaveKey);
      if (!packageNames)
        packageNames = [];

      const {success, values} = payload;
      values.forEach(value => {
        let findIndex = -1;
        for (let i = 0; i < packageNames.length; i++) {
          if (packageNames[i] == value.packageName){
            findIndex = i;
            break;
          }
        }
        if (findIndex != -1)
          packageNames.splice(findIndex,1);
      });

      Store.setStore(Const.logSaveKey, packageNames);

      yield put({
        type: 'getPageInfo',
        payload: {}
      });
    }
  }

}

function getBaseUrl() {
  return "/mlnx_data/bp";
}

